<?php
require_once("../../resources/commons.php");
require_once("../../data/User.php");
require_once("../../data/Report.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/textarea.php");
require_once("HTML/QuickForm/hidden.php");
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/static.php");

/**
 * Report creation/edition form.<br/>
 * Form to be used by a user to add a comment to a report.
 */
class WeeklyReportPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;
		
        $l_title=new HTML_Quickform_static("IndividualReport");
        $l_title->setText("<h2>Individual report</h2>");
        $this->addElement($l_title);
        
		$l_notesInput=new HTML_QuickForm_textarea("notes");
		$l_notesInput->setLabel("Personal report:");
		$l_notesInput->setCols(60);
		$l_notesInput->setRows(5);
		$this->addElement($l_notesInput);
		
		$l_reportIdInput=new HTML_QuickForm_hidden("reportId");
		$this->addElement($l_reportIdInput);
		
        $l_title=new HTML_Quickform_static("ActivityReports");
        $l_title->setText("<h2>Activity reports (one per activity)</h2>");
        $this->addElement($l_title);
		
		$l_myUserId=SessionManager::getCurrentLoggedUserId();
		$l_user=User::getFromPrimaryKey($l_myUserId);
		$l_openedActivities=$l_user->getOpenActivities();
		foreach($l_openedActivities as $l_openedActivity){
			$l_reportItemInput=new HTML_QuickForm_textarea("reportItem" . base64_encode($l_openedActivity->getId()));
			$l_reportItemInput->setLabel($l_openedActivity->getTitle() . " report");
			$l_reportItemInput->setCols(60);
			$l_reportItemInput->setRows(5);
			$this->addElement($l_reportItemInput);
		}
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		$l_cancel=new HTML_QuickForm_submit($this->getButtonName("cancel"));
		$l_cancel->setValue("Cancel");
		$this->addElement($l_cancel);
		
		//The validation rules (either client and server)
		$this->addRule("notes","Report must not be empty","required",null,"server");
		
		$this->applyFilter("notes","trim");
		
        $this->setDefaultAction("submit");
    }
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){   	
    	SessionManager::getInstance()->checkSession();
    	displayTop("Create or update the weekly report","../../resources/htmledit.php",null);
        $page->display();
        displayBottom();
    }
}

/**
 * Action to be called after a submit action.<br>
 * Send an invitation to the user with a link
 * to be used to access the registration screen.
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	SessionManager::getInstance()->checkSession();
    	
    	//Actions can be commented out only by the creator of the action or the user allocated with the action
    	$l_reportId=$page->exportValue("reportId");
    	if(isset($l_reportId) && trim($l_reportId)!=""){
    		$l_report=Report::getFromPrimaryKey(base64_decode($l_reportId));
    	}else{
    		$l_report=new Report();
    	}
    	$l_report->setUser_Id(SessionManager::getCurrentLoggedUserId());
    	$l_report->setNotes($page->exportValue("notes"));
    	$l_report->setCreatedDate(date("Y/m/d", time()));
    	$l_report->setValidated(false);
    	$l_report->save();
    	
    	//Create or update the report items
		$l_myUserId=SessionManager::getCurrentLoggedUserId();
		$l_user=User::getFromPrimaryKey($l_myUserId);
		$l_openedActivities=$l_user->getOpenActivities(); //Get opened activities
		//Save existing report items
    	$l_reportItems=$l_report->getItems(); //Get previously saved report items
    	foreach($l_reportItems as $l_reportItem){
    		unset($l_openedActivities[$l_reportItem->getActivity_Id()]);
    		$l_reportItem->setDescription($page->exportValue("reportItem" . base64_encode($l_reportItem->getActivity_Id())));
    		$l_reportItem->save();
    	}
    	//Save newly provided report items
    	foreach($l_openedActivities as $l_openedActivity){
    		$l_reportItemDescription=trim($page->exportValue("reportItem" . base64_encode($l_openedActivity->getId())));
    		if($l_reportItemDescription!="" && trim(str_replace("&nbsp;","",strip_tags($l_reportItemDescription)))!=""){
	    		$l_reportItem=new Item();
	    		$l_reportItem->setActivity_id($l_openedActivity->getId());
	    		$l_reportItem->setTitle("Report for the activity " . $l_openedActivity->getTitle());
	    		$l_reportItem->setDescription($l_reportItemDescription);
	    		$l_reportItem->setCreated(date("Y/m/d", time()));
	    		$l_reportItem->setTargetDate(date("Y/m/d", time()));
	    		$l_reportItem->setItemType_id(Item::REPORTITEM_TYPE);
	    		$l_reportItem->setItemStatus_id(Item::REPORTITEM_STATUS_CREATED);
	    		$l_reportItem->setCreatingUser_id($l_myUserId);
	    		$l_reportItem->setReport_id($l_report->getId());
	    		$l_reportItem->save();
    		}
    	}
    	
    	HTTP::redirect("index.php");
    }
}

class CancelProcess extends HTML_QuickForm_Action{
    function perform($page){
    	HTTP::redirect("index.php");
    }
}

$controller =new HTML_QuickForm_Controller('WeeklyReportProcess', false);
$controller->container(true);
$controller->addPage(new WeeklyReportPage('weeklyReportPage'));

//Look for a report for this week
$l_user=User::getFromPrimaryKey(SessionManager::getCurrentLoggedUserId());
$l_weeklyReport=$l_user->getLastUnvalidatedReport();
if(isset($l_weeklyReport)){
	$l_defaults=array(
			"reportId" => base64_encode($l_weeklyReport->getId()),
			"notes" => $l_weeklyReport->getNotes());
	$l_reportItems=$l_weeklyReport->getItems();
	foreach($l_reportItems as $l_reportItem){
		$l_defaults["reportItem" . base64_encode($l_reportItem->getActivity_Id())]=$l_reportItem->getDescription();
	}
	$controller->setDefaults($l_defaults);
}

$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());
$controller->addAction('cancel', new CancelProcess());
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());

$controller->run();

?>